package com.changgou.goods.controller;

import com.changgou.core.controller.AbstractCoreController;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Spu;
import com.changgou.goods.service.SpuService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品表（华为P40） 商品的单位
 *
 * @author liyipeng
 * @date 2021-06-26 17:00
 */
@RestController
@RequestMapping("/spu")
@CrossOrigin
public class SpuController extends AbstractCoreController<Spu> {

    private SpuService spuService;

    @Autowired
    public SpuController(SpuService spuService) {
        super(spuService, Spu.class);
        this.spuService = spuService;
    }

    /***
     * 添加Goods
     * @param goods
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Goods goods) {
        spuService.saveGoods(goods);
        return new Result(true, StatusCode.OK, "保存成功");
    }

    /***
     * 根据ID查询Goods
     * @param spuId
     * @return
     */
    @GetMapping("/goods/{id}")
    public Result<Goods> findGoodsById(@PathVariable(name = "id") Long spuId) {
        //根据ID查询Goods(SPU+SKU)信息
        Goods goods = spuService.findGoodsById(spuId);
        return new Result<Goods>(true, StatusCode.OK, "查询成功", goods);
    }


    /**
     * 审核
     *
     * @param id
     * @return
     */
    @PutMapping("/audit/{id}")
    public Result audit(@PathVariable Long id) {
        spuService.audit(id);
        return new Result(true, StatusCode.OK, "审核成功");
    }

    /**
     * 下架
     *
     * @param id
     * @return
     */
    @PutMapping("/pull/{id}")
    public Result pull(@PathVariable(name = "id") Long id) {
        spuService.pull(id);
        return new Result(true, StatusCode.OK, "下架成功");
    }

    /**
     * 批量上架
     *
     * @param ids
     * @return
     */
    @PutMapping("/pull/many")
    public Result pullMany(@RequestBody Long[] ids) {
        int count = spuService.pullMany(ids);
        return new Result(true, StatusCode.OK, "下架" + count + "个商品");
    }

    /**
     * 商品上架
     *
     */
    @PutMapping("/put/{id}")
    public Result put(@PathVariable(name = "id") Long id) {
        spuService.put(id);
        return new Result(true, StatusCode.OK, "上架成功");
    }

    /**
     * 批量上架
     *
     * @param ids
     * @return
     */
    @PutMapping("/put/many")
    public Result putMany(@RequestBody Long[] ids) {
        int count = spuService.putMany(ids);
        return new Result(true, StatusCode.OK, "上架" + count + "个商品");
    }

    /**
     * 逻辑删除
     *
     */
    @DeleteMapping("/logic/delete/{id}")
    public Result logicDelete(@PathVariable(name = "id") Long spuId) {
        spuService.logicDelete(spuId);
        return new Result(true, StatusCode.OK, "逻辑删除成功！");
    }

    /**
     * 恢复数据
     *
     */
    @PutMapping("/restore/{id}")
    public Result restore(@PathVariable(name = "id") Long spuId) {
        spuService.restore(spuId);
        return new Result(true, StatusCode.OK, "数据恢复成功！");
    }



}
